package com.grace.cms.general.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import com.grace.common.enums.GraceServerRoot;

/**
 * 加载任意目录模板
 * 模板优先级：
 * 查找非ClassLoader 内的资源
 * 查找ClassLoader templates/themes 目录下的资源
 * 查找ClassLoader templates 目录下的资源
 * 
 */
@Configuration
public class ThymeleafConfigration {

    /**
     * ClassLoader加载
     */
    @Bean
    public ClassLoaderTemplateResolver thirdTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/themes/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        // 越小优先级别越高
        templateResolver.setOrder(2);
        templateResolver.setCheckExistence(true);
        return templateResolver;
    }

    @Bean
    public SpringResourceTemplateResolver firstTemplateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("file:////" + GraceServerRoot.THEME_DIR.getWorkRoot() + "/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setOrder(0);
        templateResolver.setCheckExistence(true);
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    // @Bean
    // public ClassLoaderTemplateResolver secondTemplateResolver() {
    // ClassLoaderTemplateResolver templateResolver = new
    // ClassLoaderTemplateResolver();
    // templateResolver.setPrefix("file:////"+GraceConfig.getWorkPath()+"/themes/");
    // templateResolver.setSuffix(".html");
    // templateResolver.setTemplateMode(TemplateMode.HTML);
    // templateResolver.setCharacterEncoding("UTF-8");
    // templateResolver.setOrder(0);
    // templateResolver.setCheckExistence(true);
    // templateResolver.setCacheable(false);
    // System.out.println("+++++++{PPPPPPPPPPPPPPPPPPPPPPPPPP");
    // return templateResolver;

    // }

}